import grace from "../../../utils/grace.js";
import {Favorite} from 'favorite-model.js';

let favorite = new Favorite();

grace.page({

    /**
     * 页面的初始数据
     */
    data: {
        favorites: []
    },
    onShow() {
        let token = wx.getStorageSync('token');
        // token校验
        if (!token) {
            wx.removeStorageSync('userInfo');
            wx.navigateTo({
                url: '/pages/common/authorize/index'
            })
        }else{
            // 查询我的收藏
            favorite.getFavoriteData({'token': wx.getStorageSync('token'), 'partner_id': wx.getStorageSync('partner_id')}, (data) => {
                if(data != undefined){
                    this.$data.favorites = data;
                }
            })
        }
    },
    deleteItem(e) {
        favorite.deleteFavoriteData({'favorite_id': e.currentTarget.dataset.id, 'token': wx.getStorageSync('token')}, (data) => {
            if (data.code == 0) {
                for (let i = 0; i < this.$data.favorites.length; i++) {
                    if (this.$data.favorites[i].id == e.currentTarget.dataset.id) {
                        this.$data.favorites.splice(i, 1);
                    }
                }
            }
        })
    }
})